<template>
  <div>
    <van-nav-bar title="账号登录" left-arrow @click-left="$router.go(-1)" />
    <van-form @submit="onSubmit" validate-trigger="onSubmit">
      <van-field
        v-model="username"
        name="username"
        placeholder="用户名"
        :rules="[
          { required: true, message: '用户名不能为空' },
          { pattern: /^[\w-]{5,16}$/, message: '用户名不能少于5位' }
        ]"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        placeholder="密码"
        :rules="[
          { required: true, message: '密码不能为空' },
          { pattern: /^[\w-]{6,13}$/, message: '密码不能少于6位' }
        ]"
      />
      <div style="margin: 16px">
        <van-button block type="info" native-type="submit">登录</van-button>
      </div>
    </van-form>
    <router-link class="link" to="/register">还没有账号?去注册吧~</router-link>
  </div>
</template>

<script>
import { login } from '@/api/user'
import { setToken } from '@/utils/storage'
export default {
  name: 'LoginPage',
  data () {
    return {
      username: '',
      password: ''
    }
  },
  methods: {
    async onSubmit (values) {
      console.log('submit', values)
      const data = await login(values)
      console.log(data)
      this.$toast.success('登录成功!')
      setToken(data.body.token)
      this.$router.push('/layout/index')
    }
  }
}
</script>

<style scoped lang="less">
.link {
  color: #069;
  font-size: 12px;
  padding-right: 20px;
  float: right;
}
.van-bar {
  em {
    color: #999;
  }
}
/deep/.van-nav-bar__content {
  background-color: #21b97a;

  .van-nav-bar__title {
    color: #fff;
    font-size: 18px;
  }
  .van-icon-arrow-left {
    color: #fff;
  }
}
</style>
